/*
   Copyright (c) 2023 李伟国
   shiro-learner is licensed under Mulan PSL v2.
   You can use this software according to the terms and conditions of the Mulan PSL v2. 
   You may obtain a copy of Mulan PSL v2 at:
            http://license.coscl.org.cn/MulanPSL2 
   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.  
   See the Mulan PSL v2 for more details. 
*/

package cn.platform.service.impl;

import cn.platform.constant.enumeration.StatusConstantEnum;
import cn.platform.dto.PermissionChildren;
import cn.platform.entity.Permission;
import cn.platform.entity.RolePermission;
import cn.platform.mapper.PermissionMapper;
import cn.platform.mapper.RolePermissionMapper;
import cn.platform.service.IPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {
    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public List<Permission> findByRoleId(Long roleId) {
        List<RolePermission> rolePermissions = rolePermissionMapper.selectByRoleId(roleId);
        List<Permission> permissions = new ArrayList<>();
        for(RolePermission rolePermission : rolePermissions){
            Permission permission = this.baseMapper.selectById(rolePermission.getPermissionId());
            permissions.add(permission);
        }
        return permissions;
    }

    @Override
    public List<PermissionChildren> getPermissionTree() {
        List<PermissionChildren> permissions = this.baseMapper.selectByLevels(0, null);
        if(ObjectUtils.isEmpty(permissions)){
            return null;
        }
        for(PermissionChildren permission : permissions){
            permission.setChildren(createPermissionTreeByCode(permission, null, null));
        }
        return permissions;
    }

    public List<PermissionChildren> createPermissionTreeByCode(PermissionChildren permissionChildren, Boolean isMenu,Long userId){
        if(permissionChildren == null){
            return null;
        }
        List<PermissionChildren> permissionChildrenList = this.baseMapper.selectByPcode(permissionChildren.getCode(), isMenu, userId);
        if(permissionChildrenList == null){
            permissionChildren.setChildren(null);
            return null;
        }
        for(PermissionChildren p:permissionChildrenList){
            List<PermissionChildren> permissionTreeByCode = createPermissionTreeByCode(p, isMenu, userId);
            p.setChildren(permissionTreeByCode);
        }

        return permissionChildrenList;
    }

    @Override
    public List<PermissionChildren> getMenuTree() {
        List<PermissionChildren> permissions = this.baseMapper.selectByLevels(0, 1);
        if(ObjectUtils.isEmpty(permissions)){
            return null;
        }
        for(PermissionChildren permission : permissions){
            permission.setChildren(createPermissionTreeByCode(permission, true, null));
        }
        return permissions;
        // TODO: 2023/2/12  增加菜单树
    }

    @Override
    public Boolean delete(Permission permission) {
        if(permission == null){
            return false;
        }
        Permission permissionOld = this.baseMapper.selectById(permission.getId());
        if(!permission.equals(permissionOld)){
            return false;
        }
        return this.baseMapper.updateStatusById(StatusConstantEnum.DELETED.getCode(), permission.getId()) > 0;
    }

    @Override
    public Boolean reset(Permission permission) {
        if(permission == null){
            return false;
        }
        Permission permissionOld = this.baseMapper.selectById(permission.getId());
        if(!permission.equals(permissionOld)){
            return false;
        }
        return this.baseMapper.updateStatusById(StatusConstantEnum.ENABLE.getCode(), permission.getId()) > 0;
    }

    @Override
    public List<PermissionChildren> getUserMenuByUserId(Long userId) {
        // TODO: 2023/2/20 用户菜单
        List<PermissionChildren> permissions = this.baseMapper.selectMenuByUserId(userId, 0);
        if(ObjectUtils.isEmpty(permissions)){
            return null;
        }
        for(PermissionChildren permission : permissions){
            permission.setChildren(createPermissionTreeByCode(permission,true, userId));
        }
        return permissions;
    }

    @Override
    public List<String> getUserListByUserId(Long userId) {
        return this.baseMapper.selectListByUserId(userId);
    }
}
